# --*-- coding: UTF-8 --*--
from . import *

'''
   记录日志到文本文件
   每天生成一个文本文件
   每天传递过来的日志添加前缀：HHmmss-$type-
'''


class Log:
    def error(self, msg):
        path = os.path.dirname(__file__)
        now = datetime.datetime.now()
        date_str = now.strftime('%Y%m%d').rsplit('common')[0]
        file_name = '{}/errLog/{}.log'.format(path, date_str)
        if not isinstance(msg, str):
            msg = str(msg)
        msg = now.strftime('%H:%M:%S') + '-ERR: ' + msg + '\n'
        try:
            with open(file_name, 'a') as f:
                f.write(msg)
                f.flush()
        except FileNotFoundError as e:
            with open(file_name, 'w') as f:
                if isinstance(msg, str):
                    f.write(msg)
                    f.flush()


def log(msg):
    now = datetime.datetime.now()
    date_str = now.strftime('%Y%m%d')
    path = os.path.dirname(__file__).rsplit('common')[0]
    f = open('{}/errLog/{}.log'.format(path, date_str), 'a')
    if type(msg) != str:
        msg = str(msg)
    msg = now.strftime('%H:%M:%S: ') + msg + '\n'
    f.write(msg)
